package com.service;


import com.dao.PersonnelJdbc;
import com.model.Personnel;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PersonnelService {
    PersonnelJdbc pj=new PersonnelJdbc();
    public List<Personnel> show() {
        List<Personnel> list=new ArrayList<Personnel>();
        ResultSet rs=pj.show();
        try {
            while(rs.next()){
                int pid = rs.getInt("pid");
                String pname = rs.getString("pname");
                int age = rs.getInt("age");
                String sex = rs.getString("sex");
                String pgroup = rs.getString("pgroup");
                String pposition = rs.getString("pposition");
                Date entry_days = rs.getDate("entry_days");
                Personnel personnel = new Personnel(pid, pname, age, sex, pgroup, pposition,entry_days);
                list.add(personnel);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    public void add(String pname, String age, String sex, String pgroup, String pposition, String entry_days) {
        pj.add(pname,age,sex,pgroup,pposition,entry_days);
    }

    public void delete(String id) {
        pj.delete(id);
    }

    public Personnel toupdate(String id) {
        ResultSet r3=pj.show();
        Personnel personnel= new Personnel();
        try {
            while (r3.next()) {
                int pid= r3.getInt("pid");
                if(id.equals(pid+"")){
                    String pname = r3.getString("pname");
                    int age = r3.getInt("age");
                    String sex = r3.getString("sex");
                    String pgroup = r3.getString("pgroup");
                    String pposition = r3.getString("pposition");
                    Date entry_days = r3.getDate("entry_days");
                    personnel=new Personnel(pid,pname,age,sex,pgroup,pposition,entry_days);
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return personnel;
    }

    public void update(String pid, String pname, String age, String sex, String pgroup, String pposition, String entry_days) {
        pj.update(pid,pname,age,sex,pgroup,pposition,entry_days);
    }
}
